require 'event_editor/io'
require 'event_editor/character_image_panel'
require 'event_editor/character_selecter'

module EventEditor
  class CharacterSelectDialog

     GRID_WIDTH = 24
     GRID_HEIGHT = 32
     def self.show(base, model, id, sx, sy, &func)
        @model = model
       dialog = Gtk::Dialog.new("Character Selection",
               base,
               Gtk::Dialog::MODAL |
               Gtk::Dialog::DESTROY_WITH_PARENT,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        # init components
        list_model = Gtk::ListStore.new(String, String)
        list_box = Gtk::TreeView.new(list_model)
        selection = list_box.selection
        self.add_columns(list_box)

        character_selecter = CharacterSelecter.new
        @character_image_panel = CharacterImagePanel.new(character_selecter)
        
        # packing
        dialog.set_size_request(600, 400)
        hbox = Gtk::HBox.new(false, 5)
        hbox.set_border_width(5)
        list_box.set_width_request(150)
        hbox.pack_start(list_box, false, false)
        
        vbox = Gtk::VBox.new
        vbox.add(@character_image_panel)
        vbox.pack_start(self.charaset_control(selection), false, false)
        
        hbox.add(vbox)

        dialog.vbox.add(hbox)
        dialog.vbox.show_all

        # selection signals
        
        selection.signal_connect('changed') do
          if selection.selected
            if selection.selected
              self.load(selection.selected[1])
            else
              self.clear
            end
          end
        end

        # initialize
        arr = self.refresh_list(list_model)
        
        t = arr.index(id)
        selection.select_path(Gtk::TreePath.new(t + 1)) if t


        self.load(id, sx, sy)


        # run
        dialog.run do |response|
          if response == Gtk::Dialog::RESPONSE_OK
            func.call(selection.selected[1], character_selecter.sx, character_selecter.sy)
          end
          dialog.destroy
        end
      end
      
      def self.charaset_control(selection)
        hbox = Gtk::HBox.new
        hbox.add(Gtk::Label.new('Size', true))
        
        # Map X Input
        @grid_w_input = Gtk::SpinButton.new(1, 99, 1)
        @grid_w_input.width_request = 40
        @grid_w_input.value = 10
        hbox.add(@grid_w_input)
        
        hbox.add(Gtk::Label.new(' x ', true))
        
        @grid_h_input = Gtk::SpinButton.new(1, 99, 1)
        @grid_h_input.width_request = 40
        @grid_h_input.value = 10
        hbox.add(@grid_h_input)

        hbox.add(Gtk::Label.new(' Frames', true))
        @anum_input = Gtk::SpinButton.new(1, 99, 1)
        @anum_input.width_request = 20
        @anum_input.value = 3
        hbox.add(@anum_input)
        
        hbox.add(Gtk::Label.new(' Direction', true))
        @dir_combobox = ::Gadgets::Combobox.new([['1', 1], ['2', 2], ['4', 4], ['8', 8]], [String, Integer])
        hbox.add(@dir_combobox)
        
        btn = Gtk::Button.new("Set")
        btn.signal_connect('pressed') do
          t = selection.selected[1]
          if t
            setting = @model.character_chipsets_setting
            setting[t]['gw'] = @grid_w_input.value.floor
            setting[t]['gh'] = @grid_h_input.value.floor
            setting[t]['anime_num'] = @anum_input.value.floor
            setting[t]['dir_num'] = @dir_combobox.selected_data.floor
            @model.reset_character_chipset(t)
            self.load(t)
          end
        end
        hbox.add(btn)
        
        return hbox
      end
      
      def self.add_columns(treeview)
        renderer = Gtk::CellRendererText.new

        column = Gtk::TreeViewColumn.new('Name', renderer, "text" => 0)
        column.sizing = Gtk::TreeViewColumn::FIXED
        column.fixed_width = 50
        treeview.append_column(column)
      end
      
      def self.refresh_list(model)
        model.clear
        arr = IO.get_character_images
        
        iter = model.append
        iter[0] = "none"
        iter[1] = nil
        
        
        arr.each do |name|
          t = @model.character_chipsets_setting[name]
          unless t
            @model.character_chipsets_setting[name] = {'gw' => 24, 'gh' => 32, 'anime_num' => 3, 'anime_type' => 0, 'dir_num' => 4}
          end
          iter = model.append
          iter[0] = name
          iter[1] = name
        end
        return arr
      end
      
      def self.clear
        @character_image_panel.load(nil, GRID_WIDTH, GRID_HEIGHT)
      end
      
      def self.load(id, sx = 0, sy = 0)
        if id
          gw = @model.character_chipsets_setting[id]['gw']
          gh = @model.character_chipsets_setting[id]['gh']
          
          setting = @model.character_chipsets_setting
          @grid_w_input.value = gw
          @grid_h_input.value = gh
          @anum_input.value = setting[id]['anime_num']
          @dir_combobox.select(setting[id]['dir_num'])
          
          @character_image_panel.load(Texture.load($res.image_path + "game/character/" + id), gw, gh, sx, sy)
        else
          self.clear
        end
      end
  end
end